package com.by.common.redis;

/**
 * Redis键名工具类
 * 用于统一管理Redis键名，避免硬编码
 */
public class RedisKeyUtils {

    // ==================== 限流相关键 ====================
    /** 通用限流键前缀 */
    private static final String RATE_LIMIT_PREFIX = "rate:limit:";

    /** 订单服务限流键 */
    private static final String ORDER_SERVICE_RATE_LIMIT = RATE_LIMIT_PREFIX + "order-service";

    /** 商品限流键前缀 */
    private static final String GOODS_RATE_LIMIT_PREFIX = RATE_LIMIT_PREFIX + "goods:";

    /** 秒杀限流键前缀 */
    private static final String SECKILL_RATE_LIMIT_PREFIX = RATE_LIMIT_PREFIX + "seckill:";

    // ==================== 库存相关键 ====================
    /** 秒杀库存键前缀 */
    private static final String SECKILL_STOCK_PREFIX = "seckill:stock:";
    
    /** 秒杀预扣减库存键前缀 */
    private static final String SECKILL_RESERVED_STOCK_PREFIX = "seckill:reserved:stock:";

    // ==================== 订单相关键 ====================
    /** 订单过期键前缀 */
    private static final String ORDER_EXPIRE_PREFIX = "order:expire:";

    /** 秒杀用户记录键前缀 */
    private static final String SECKILL_USER_RECORD_PREFIX = "seckill:user:";

    /** 秒杀用户集合键前缀 */
    private static final String SECKILL_USER_SET_PREFIX = "seckill:users:";

    /** 秒杀订单键前缀 */
    private static final String SECKILL_ORDER_PREFIX = "seckill:order:";
    
    // ==================== 分布式锁相关键 ====================
    /** 订单创建分布式锁前缀 */
    private static final String ORDER_CREATE_LOCK_PREFIX = "lock:order:create:";
    
    /** 秒杀订单创建分布式锁前缀 */
    private static final String SECKILL_CREATE_LOCK_PREFIX = "lock:seckill:create:";

    // ==================== 限流键生成方法 ====================

    /**
     * 生成订单服务限流键
     * @return 限流键
     */
    public static String getOrderServiceRateLimitKey() {
        return ORDER_SERVICE_RATE_LIMIT;
    }

    /**
     * 生成特定商品的限流键
     * @param goodsId 商品ID
     * @return 限流键
     */
    public static String getGoodsRateLimitKey(Long goodsId) {
        return GOODS_RATE_LIMIT_PREFIX + goodsId;
    }

    /**
     * 生成秒杀接口限流键
     * @param goodsId 商品ID
     * @return 限流键
     */
    public static String getSeckillRateLimitKey(Long goodsId) {
        return SECKILL_RATE_LIMIT_PREFIX + goodsId;
    }

    // ==================== 库存键生成方法 ====================

    /**
     * 生成秒杀库存键
     * @param goodsId 商品ID
     * @return 库存键
     */
    public static String getSeckillStockKey(Long goodsId) {
        return SECKILL_STOCK_PREFIX + goodsId;
    }
    
    /**
     * 生成秒杀预扣减库存键
     * @param goodsId 商品ID
     * @param userId 用户ID
     * @return 预扣减库存键
     */
    public static String getSeckillReservedStockKey(Long goodsId, Long userId) {
        return SECKILL_RESERVED_STOCK_PREFIX + goodsId + ":user:" + userId;
    }

    // ==================== 订单键生成方法 ====================

    /**
     * 生成订单过期键
     * @param orderId 订单ID
     * @return 过期键
     */
    public static String getOrderExpireKey(Long orderId) {
        return ORDER_EXPIRE_PREFIX + orderId;
    }

    /**
     * 生成秒杀用户记录键
     * @param userId 用户ID
     * @param goodsId 商品ID
     * @return 用户记录键
     */
    public static String getSeckillUserRecordKey(Long userId, Long goodsId) {
        return SECKILL_USER_RECORD_PREFIX + userId + ":goods:" + goodsId;
    }

    /**
     * 生成秒杀用户集合键
     * @param goodsId 商品ID
     * @return 用户集合键
     */
    public static String getSeckillUserSetKey(Long goodsId) {
        return SECKILL_USER_SET_PREFIX + goodsId;
    }

    /**
     * 生成秒杀订单键
     * @param userId 用户ID
     * @param goodsId 商品ID
     * @return 订单键
     */
    public static String getSeckillOrderKey(Long userId, Long goodsId) {
        return SECKILL_ORDER_PREFIX + userId + ":goods:" + goodsId;
    }
    
    /**
     * 生成用户秒杀限流键
     * @param userId 用户ID
     * @param goodsId 商品ID
     * @return 用户秒杀限流键
     */
    public static String getUserSeckillLimitKey(Long userId, Long goodsId) {
        return SECKILL_RATE_LIMIT_PREFIX + goodsId + ":user:" + userId;
    }
    
    /**
     * 生成订单创建分布式锁键
     * @param goodsId 商品ID
     * @return 分布式锁键
     */
    public static String getOrderCreateLockKey(Long goodsId) {
        return ORDER_CREATE_LOCK_PREFIX + goodsId;
    }
    
    /**
     * 生成秒杀订单创建分布式锁键
     * @param goodsId 商品ID
     * @return 分布式锁键
     */
    public static String getSeckillCreateLockKey(Long goodsId) {
        return SECKILL_CREATE_LOCK_PREFIX + goodsId;
    }
}